package com.yaj.myfactorysupport.schedule;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yaj.myfactorysupport.service.TLightInfoAccompService;
import com.yaj.myfactorysupport.util.RespBean;
import com.yaj.myfactorysupport.websocket.WebSocketHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @ClassName ScheduleTask
 * @Description 定时任务类
 * @date 2022/1/9 17:48
 * @Version 1.0
 */
@Component
@Slf4j
public class ScheduleTask {
    @Autowired
    private TLightInfoAccompService tLightInfoAccompService;

    @Autowired
    private WebSocketHandler webSocketHandler;

    @Scheduled(cron = "*/15 * * * * ?")
    public void checkLightInfo(){
        RespBean resp = tLightInfoAccompService.getTLightInfoAccompList();
        //JSONObject jsonObject = JSONUtil.parseObj(resp);
        //jsonObject.setDateFormat("yyyy-MM-dd HH:mm:ss");
        //webSocketHandler.sendAll(jsonObject.toString());
        ObjectMapper mapper = new ObjectMapper();
        String json= null;
        try {
            json = mapper.writeValueAsString(resp);
            webSocketHandler.sendAll(json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}
